Completed
Push — master ( d4303e...fcef0c )
by Jan
15s queued 12s
created

FrameBuilder.appendTerminatedTextWithFrameEncoding   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
1
import { TextEncoding } from "./definitions/Encoding"
2
import { stringToEncodedBuffer } from "./util-text-encoding"
3
4
type Size = { size: number }
5
6
export class FrameBuilder {
7
    private identifier: string
8
    private encoding: TextEncoding = TextEncoding.ISO_8859_1
9
    private buffer = Buffer.alloc(0)
10
11
    // TODO remove identifier
12
    constructor(
13
        identifier: string,
14
        encoding?: TextEncoding
15
    ) {
16
        this.identifier = identifier
17
        if (encoding !== undefined) {
18
            this.encoding = encoding
19
            this.appendNumber(encoding, {size: 1})
20
        }
21
    }
22
23
    appendBuffer(buffer: Buffer) {
24
        this.buffer = Buffer.concat([this.buffer, buffer])
25
        return this
26
    }
27
28
    appendNumber(value: number, {size}: Size) {
29
        if (!Number.isInteger(value)) {
30
            throw new RangeError("An integer value is expected")
31
        }
32
        let hexValue = value.toString(16)
33
        if (hexValue.length % 2 !== 0) {
34
            hexValue = "0" + hexValue
35
        }
36
        const valueBuffer = Buffer.from(hexValue, 'hex')
37
        const zeroPad = Buffer.alloc(size - valueBuffer.length)
38
        return this.appendBuffer(
39
            Buffer.concat([zeroPad, valueBuffer]).subarray(0, size)
40
        )
41
    }
42
43
    appendText(text: string, encoding: TextEncoding = TextEncoding.ISO_8859_1) {
44
        // TODO remove the .toString() for new API release
45
        return this.appendBuffer(
46
            stringToEncodedBuffer(text.toString(), encoding)
47
        )
48
    }
49
50
    appendTextWithFrameEncoding(text: string) {
51
        return this.appendText(text, this.encoding)
52
    }
53
54
    appendTerminatedText(
55
        text: string,
56
        encoding: TextEncoding = TextEncoding.ISO_8859_1
57
    ) {
58
        // TODO remove the .toString() for new API release
59
        return this.appendText(text.toString() + "\0", encoding)
60
    }
61
62
    appendTerminatedTextWithFrameEncoding(text: string) {
63
        return this.appendTerminatedText(text, this.encoding)
64
    }
65
66
    appendArray<T>(
67
        values: T[],
68
        callback: (builder: FrameBuilder, value: T) => void
69
    ) {
70
        values.forEach(value => callback(this, value))
71
        return this
72
    }
73
74
    getBuffer() {
75
        return this.buffer
76
    }
77
78
    // TODO remove this function
79
    getBufferWithPartialHeader() {
80
        const header = Buffer.alloc(10)
81
        header.write(this.identifier, 0)
82
        header.writeUInt32BE(this.buffer.length, 4)
83
        return Buffer.concat([header, this.buffer])
84
    }
85
86
}
87